var twbOrderVm = new Vue({
    el: '#rrapp',
    data:function(){
        return {
            /*订单信息*/
            orderData:[],
            /*当前选中行信息*/
            currentRow:{},
            /*所有选中订单行*/
            selectedOrderList:[],
            /*取消订单对象*/
            erpOrderLogs:{},
            /*取消订单弹出框*/
            cancelOrderVisible:false,
            /*打印弹出框*/
            printVisible:false,
            q: {
                orderNo: '',
                memberName: '',
                projectName:'',
                shipName: '',
                shipTel:'',
               /* orderSource: '2',*/
                orderSource:'',
                productionStatus: '',
                payStatus:'',
                shipStatus:'',
                createTime:null,
                startTime: '',
                endTime: '',
                //客户名称查询
                name: '',
                //产品搜索
                productName: '',
                //查询取消订单标记
                isCancel:0,
            },
            //订单统计
            totalCount:{dayCount:0,monthCount:0,yearCount:0,dayMoney:0,monthMoney:0,yearMoney:0},
            e:{
                //订单状态:
                productionStatusEnum:[],
                //支付状态:
                payStatusEnum:[],
                //订单类型
                orderSourceEnum: [],
                twbProductionStatusEnum: [{value:'',label:'全部'},{value:'1',label:'待审核'}, {value: '3', label: '生产中'}, {
                    value: '4', label: '生产完成'}, {value: '6', label: '已完成'},{value: '11', label: '取消'}],
                twbOrderSourceEnum:[{value:'2',label:'门店订单'},{value:'1',label:'商城订单'}],
                twbPayStatusEnum:[{value:'',label:'全部'},{value:'\'0\'',label:'未付款'},{value:'1',label:'已收款'}],
                twbShipStatusEnum:[{value:'',label:'全部'},{value:'\'0\'',label:'未发货'},{value:'1',label:'已发货'}]
            },
            page: {currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0, ascending: [], descending: []},
            title: null,
        }
    } ,
    mounted: function () {
        this.query();
    },
    methods: {
        // 页数变化
        handleSizeChange: function (val) {
            this.page.pageSize = val;
            this.reload();
        },
        //当前页变化
        handleCurrentChange: function (val) {
            this.page.currentPage = val;
            this.reload();
        },
        // 查询
        query: function () {
            if(this.q.productionStatus==11){
                this.q.isCancel=1;
            }else{
                this.q.isCancel=0;
            }
            //查询参数
            var queryParam = this.q;
            // 录入时间
            var createTime = queryParam.createTime;
            if (!isBlank(createTime)){
                queryParam.startTime = createTime[0] + ' 00:00:00';
                queryParam.endTime = createTime[1] + ' 23:59:59';
            }
            this.page={currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0};
            // 分页参数
            queryParam.page = this.page.currentPage;
            queryParam.limit = this.page.pageSize;
            //ajax 请求
            var self= this;
            $.ajax({
                type: "POST",
                url: baseURL + "twbOrder/erporder/list",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(queryParam),
                success: function (r) {
                    if (r.code == 0) {
                        self.page.total = r.page.totalCount;
                        self.orderData = r.page.list;
                        self.e.productionStatusEnum=r.productionStatusEnum;
                        self.e.payStatusEnum=r.payStatusEnum;
                        self.e.orderSourceEnum=r.orderSourceEnum;
                        self.totalCount=r.totalCount;
                    } else {
                        alert(r.msg);
                    }
                }
            });

        },
        //添加订单:
        add:function(){
            window.parent.vm.zyRouter({
                url:'modules/order/addTwbOrder.html',
                name:'新增订单',
                blank: true,
                vmName:'twbAddOrderPageVm',
                params:{},
            });
        },
        //修改订单
        update:function(){
            var self=this;
            if(self.selectedOrderList.length==0){
                alert("请选中一行再进行<span style='color: red;'>修改订单</span>操作!");
                return;
            }
            if(self.selectedOrderList.length>1){
                alert("只允许选中一行进行<span style='color: red;'>修改订单</span>操作!");
                return;
            }
            var orderNo=self.selectedOrderList[0].orderNo;
            var data={'orderNo':orderNo};
            window.twbUpdateOrderPageVm.loadData(data);
        },

        //重置查询条件:
        reset() {
                 this.q.orderNo = '',
                 this.q.memberName = '',
                 this.q.shipName = '',
                 this.q.shipTel = '',
                 this.q.orderSource = '',
                 this.q.productionStatus = '',
                 this.q.startTime = '',
                 this.q.endTime = '',
                 this.q.projectName='';
                 this.q.isCancel=0;
                 this.q.createTime=null;
                 this.query();
        },
        reload:function(){
            //查询参数
            var queryParam = this.q;
            // 分页参数
            queryParam.page = this.page.currentPage;
            queryParam.limit = this.page.pageSize;
            var self=this;
            //ajax 请求
            $.ajax({
                type: "POST",
                url: baseURL + "twbOrder/erporder/list",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(queryParam),
                success: function (r) {
                    if (r.code == 0) {
                        self.page.total = r.page.totalCount;
                        self.orderData = r.page.list;
                        self.e.productionStatusEnum=r.productionStatusEnum;
                        self.e.payStatusEnum=r.payStatusEnum;
                        self.e.orderSourceEnum=r.orderSourceEnum;
                        self.totalCount=r.totalCount;
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        //批量审核
        batchAudit:function(){
            var self=this;
            if(self.selectedOrderList.length==0){
                alert("请至少选中一行再进行审核操作!");
                return;
            }
            var ids=[];
            for (var obj in self.selectedOrderList) {
                ids.push(self.selectedOrderList[obj].orderNo);
            }
            var orderNos=ids.join(',');
            this.$confirm('是否确认批量审核, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm'){
                        $.ajax({
                            type: "GET",
                            url: baseURL + "twbOrder/erporder/batchAudit?orderNos="+ orderNos,
                            contentType: "application/json",
                            data: null,
                            success: function(r){
                                if(r.code === 0){
                                    alert("操作成功!");
                                    self.reload();
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },

        //订单详情
        erpOrderDetail:function(orderNo){
            window.parent.vm.zyRouter({
                url:'modules/order/twbOrderDetail.html',
                name:'订单详情',
                blank: true,
                vmName:'twbOrderDetailVm',
                //1表示从订单列表跳过去的，2表示从月结订单跳过去的
                params:{orderNo:orderNo,type:1},
            });
        },
        //取消订单
        openCancelDialog:function(){
            var self=this;
            if(self.selectedOrderList.length==0){
                alert("请选中一行再进行<span style='color: red;'>取消订单</span>操作!");
                return;
            }
            if(self.selectedOrderList.length>1){
                alert("只允许选中一行进行<span style='color: red;'>取消订单</span>操作!");
                return;
            }
            var orderNo=self.selectedOrderList[0].orderNo;
            if(self.selectedOrderList[0].productionStatus!=3){
                alert("只有生产状态为<span style='color: red;'>生产中</span>订单才能取消!");
                return;
            }
            if(self.selectedOrderList[0].orderSource==1){
                alert("商城订单不允许进行<span style='color: red;'>取消订单</span>操作!")
                return;
            }
            self.erpOrderLogs.orderNo=orderNo;
            self.erpOrderLogs.logText='';
            self.cancelOrderVisible=true
        },
        //向后台发送请求
        cancelOrder:function(){
            var self=this;
            if(self.erpOrderLogs.logText==''){
                alert("取消原因不能为空");
                return ;
            }
             this.$confirm('是否确认取消订单?','取消订单提示', {
                 confirmButtonText: '确定',
                 cancelButtonText: '取消',
                 type: 'warning'
             }).then(function () {
                 $.ajax({
                     type: "POST",
                     url: baseURL + "twbOrder/erporder/cancelOrder",
                     contentType: "application/json",
                     data: JSON.stringify(self.erpOrderLogs),
                     success: function(r){
                         if(r.code == 0){
                             alert('操作成功', function(index){
                                 self.cancelOrderVisible=false;
                                 self.reload();
                             });
                         }else{
                             alert(r.msg);
                         }
                     }
                 });
             }).catch(function () {
                 this.$message({
                     type: 'info',
                     message: '已取消操作'
                 })
             });
        },
        //打开收银
        openReceviceMoney:function () {
            var self=this;
            if(self.selectedOrderList.length==0){
                alert("请选中一行再进行<span style='color: red;'>订单收银</span>操作!");
                return;
            }
            if(self.selectedOrderList.length>1){
                alert("只允许选中一行进行<span style='color: red;'>订单收银</span>操作!");
                return;
            }
            var row=self.selectedOrderList[0];
            if(row.onCredit==true){
                    this.$confirm('月结订单需到月结订单页面收银,是否需要打开月结订单页面?','温馨提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(function () {
                        self.toMonthOrder(row.orderNo);
                    }).catch(function () {
                        return;
                    });
                return;
            }
            window.twbColletMoneyPageVm.loadData(row);
        },
        //跳转到月结订单
        toMonthOrder:function(orderNo){
            window.parent.vm.zyRouter({
                url:'modules/order/twbMonthOrder.html',
                name:'月结订单',
                blank: true,
                vmName:'twbMonthOrderVm',
                params:{orderNo:orderNo},
            });
        },
        //生产完成
        finishOrderItem:function(){
            var self=this;
            if(self.selectedOrderList.length==0){
                alert("请至少选中一行再进行<span style='color: red;'>订单完成</span>操作!");
                return;
            }
            var ids=[];
            for (var obj in self.selectedOrderList) {
                ids.push(self.selectedOrderList[obj].orderNo);
            }
            var orderNos=ids.join(',');
            this.$confirm('是否确认订单完成?','订单完成提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                $.get(baseURL + "twbOrder/erporder/finishOrderItem?orderNos="+ orderNos, function(r) {
                    if(r.code==0){
                        alert("操作成功");
                        self.reload();
                    }else{
                        alert(r.msg);
                    }
            })
        }).catch(function () {
                self.$message({
                    type: 'info',
                    message: '已取消操作'
            })
        });
    },
         //添加物流
        addLogistic:function(){
            var self=this;
            if(self.selectedOrderList.length==0){
                alert("请选中一行再进行<span style='color: red;'>添加物流</span>操作!");
                return;
            }
            if(self.selectedOrderList.length>1){
                alert("只允许选中一行进行<span style='color: red;'>添加物流</span>操作!");
                return;
            }
            var orderNo=self.selectedOrderList[0].orderNo;
            var data={'orderNo':orderNo};
            window.twbLogisticVm.loadData(data);
        },
        //打印
        print:function(){
            var self=this;
            if(self.selectedOrderList.length==0){
                alert("请选中一行再进行<span style='color: red;'>打印</span>操作!");
                return;
            }
            if(self.selectedOrderList.length>1){
                alert("只允许选中一行进行<span style='color: red;'>打印</span>操作!");
                return;
            }
            self.printVisible=true;
        },
        printProduct:function(){
            var orderNo=this.selectedOrderList[0].orderNo;
            this.printVisible=false;
            this.$refs.zyPrintProduction.getOrderData(orderNo);
        },
        printSend:function(){
            //打印结算单
            var orderNo=this.selectedOrderList[0].orderNo;
            this.printVisible=false;
            this.$refs.zyPrintFinal.getOrderData(orderNo);
        },
        //获取所有选中行
        handleOrderSelection:function(val){
            twbOrderVm.selectedOrderList = val;
        },
        /*// 选中当前行
        handleCurrentChange:function(val){
            twbOrderVm.currentRow=val;
        },*/
        /*单击行选中*/
        clickRow:function(row){
            this.$refs.orderTable.toggleRowSelection(row);
        },
        /*格式化生产状态*/
        formatterProdStatus:function (row, column) {
            for (var i=0;i<this.e.productionStatusEnum.length;i++){
                if(row.productionStatus==this.e.productionStatusEnum[i].id){
                    return this.e.productionStatusEnum[i].describe;
                }
            }
        },
        /*格式化付款状态*/
        formatterPayStatus:function (row, column) {
            for (var i=0;i<this.e.payStatusEnum.length;i++){
                if(row.payStatus==this.e.payStatusEnum[i].value){
                    return this.e.payStatusEnum[i].describe;
                }
            }
        },
        /*格式化下单来源*/
        formatterOrderSourceStatus:function (row, column) {
            for (var i=0;i<this.e.orderSourceEnum.length;i++){
                if(row.orderSource==this.e.orderSourceEnum[i].id){
                    return this.e.orderSourceEnum[i].describe;
                }
            }
        },
        loadData:function () {
            this.reload();
        },
    },
});